package com.edou.member.controller.mobile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.base.controller.BaseController;
import com.edou.base.util.EncryptUtil;
import com.edou.member.entity.MemberUser;
import com.edou.member.form.MemberUserForm;
import com.edou.member.service.IMemberUserService;

/**
 * 会员帐号模块
 * 
 * @creator admin
 * @create-time 2016/3/12 16:10:22
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Controller("memberUserAccountMobileController")
@Scope("prototype")
@RequestMapping("/app/member/account")
public class MemberUserAccountMobileController extends BaseController {

	@Resource(name = "memberUserServiceImpl")
	private IMemberUserService memberUserService;

	/**
	 * 跳转到会员修改密码页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/password.htm", method = RequestMethod.GET)
	public String password() {
		return "/member/account/password";
	}

	/**
	 * 验证原密码是否正确
	 * 
	 * @return
	 */
	@RequestMapping(value = "/check_password.htm", method = RequestMethod.POST)
	public ModelAndView checkPassword(MemberUserForm model, HttpServletRequest request, HttpServletResponse response) {
		if (StringUtils.isBlank(model.getOldPassword())) {
			return ajaxJSON(Status.success, "false");
		}
	
		//因为数据库中的password是加密的所以将传过来的密码加密后进行比较
		MemberUser memberUser = memberUserService.getMemberUserByAccount(model.getAccount());
		if (EncryptUtil.encryptionPw(model.getOldPassword()).equals(memberUser.getPassword())) {
			return ajaxJSON(Status.success, "true");
		}
		
		return ajaxJSON(Status.success, "false");
	}

	/**
	 * 密码修改
	 * 
	 * @return
	 */
	@RequestMapping(value = "/do_update_pwd.htm", method = RequestMethod.POST)
	public ModelAndView doUpdatePwd(MemberUserForm model, HttpServletRequest request, HttpServletResponse response) {
		try {
			MemberUser memberUser = memberUserService.getMemberUserByAccount(model.getAccount());
			memberUser.setPassword(model.getPassword());
			memberUserService.updatePwd(memberUser);

			return ajaxJSON(Status.success, "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxJSON(Status.error, "修改失败");
		}
	}
	
	/**
	 * 申请实名认证
	 * 
	 * @return
	 */
	@RequestMapping(value = "/upload_real_name_cert_info.htm", method = RequestMethod.POST)
	public ModelAndView uploadRealNameCertInfo(MemberUserForm model, HttpServletRequest request, HttpServletResponse response) {
		try {
			memberUserService.uploadRealNameCertInfoById(model.getItem().getId(),model.getItem().getName(),model.getItem().getCertIdcard(),model.getItem().getCertIdcardPath());
			return ajaxJSON(Status.success, "申请成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxJSON(Status.error, "申请失败");
		}
	}

}